package priv.yiyuan.example.design.flyweight;

import java.util.HashMap;
import java.util.Map;

/**
 * 资源工厂
 * <p>
 * 享元模式-享元工厂
 *
 * @author YiYuan
 * @data 2025/9/16
 * @apoNote
 */
public class CourseResourceFactory {

    // 将大文件保存，变相的减少了 大文件在内存中的数量，而每个用户仅仅是 进度不同及播放设置不同。
    private static final Map<String, CourseResource> resourceMap = new HashMap<>();

    public static CourseResource getResource(String type) {

        CourseResource resource = resourceMap.get(type);
        if (resource == null) {
            switch (type) {
                case "Video":
                    resource = new VideoResource(type);
                    break;
                case "Document":
                    resource = new DocumentResource(type);
                    break;
                case "Quiz":
                    resource = new QuizResource(type);
                    break;
                default:
                    throw new IllegalArgumentException("未知资源类型: " + type);
            }
            resourceMap.put(type, resource);
        }

        return resource;
    }

    public static int getResourceCount() {
        return resourceMap.size();
    }

}
